package services

import (
	"regexp"
)

// 关键词：Overtaken、Abandoned、Overtaken.、Overtake
var keywordMap = map[string]string{
	"Overtaken":                       "占领成功",
	"Abandoned on overtake":           "占领时取消",
	"Overtake started":                "开始占领",
	"Overtake canceled":               "取消占领",
	"Overtaken (Left squad/Promoted)": "占领，离开小队/晋升",
}

type LandFlagRule struct {
	ruleMap map[string]string
}

/**
    ActionTime    string `gorm:"column:action_time;type:datetime(3);not null"`
	UserId        uint64 `gorm:"column:user_id;type:bigint(20) unsigned;not null"`
	UserName      string `gorm:"column:user_name;type:varchar(255);not null"`
	StreamId      string `gorm:"column:stream_id;type:varchar(255);not null"`
	FlagType      string `gorm:"column:flag_type;type:varchar(255);not null"`
	FlagId        uint64 `gorm:"column:flag_id;type:bigint(20) unsigned;not null"`
	Location      string `gorm:"column:location;type:varchar(255);not null"`
	OwnerId       uint64 `gorm:"column:owner_id;type:bigint(20) unsigned;not null"`
	OwnerName     string `gorm:"column:owner_name;type:varchar(255);not null"`
	OwnerStreamId string `gorm:"column:owner_stream_id;type:varchar(255);not null"`
*/

// 前19个字符,日期时间
func (r *LandFlagRule) GetActionTime(str string) string {
	return str[:19]
}

func (r *LandFlagRule) HandleFields(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	regKeyword := regexp.MustCompile(`\[Flag]\s+(.*?)\.\s`)
	sliceKeywords := regKeyword.FindStringSubmatch(str)
	if len(sliceKeywords) > 0 {
		switch sliceKeywords[1] {
		case "Overtaken":
			actionTime, userId, userName, streamId,
				flagType, flagId, location, ownerId, ownerName, ownerStreamId =
				r.HandleOvertaken(str)
		case "Abandoned on overtake":
			actionTime, userId, userName, streamId,
				flagType, flagId, location, ownerId, ownerName, ownerStreamId =
				r.HandleAbandoned(str)
		case "Overtake started":
			actionTime, userId, userName, streamId,
				flagType, flagId, location, ownerId, ownerName, ownerStreamId =
				r.HandleOvertakeStart(str)
		case "Overtake canceled":
			actionTime, userId, userName, streamId,
				flagType, flagId, location, ownerId, ownerName, ownerStreamId =
				r.HandleOvertakeCancel(str)
		case "Overtaken (Left squad/Promoted)":
			actionTime, userId, userName, streamId,
				flagType, flagId, location, ownerId, ownerName, ownerStreamId =
				r.HandleOvertakenLeft(str)
		}
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 转让
func (r *LandFlagRule) HandleOvertaken(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//New owner: 76561198432006211(328, 老六). Old owner: 76561199509002923(210, 黎祖宗). FlagId: 13144. Location: X=554300.375 Y=-408039.531 Z=2637.034
	reg := regexp.MustCompile(`New owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+Old owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\.\s+Location:\s+(.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[3]
		streamId = strslice[1]
		flagType = "Overtaken"
		flagId = strslice[7]
		location = strslice[8]
		ownerId = strslice[5]
		ownerName = strslice[6]
		ownerStreamId = strslice[4]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 占领成功Abandoned
func (r *LandFlagRule) HandleAbandoned(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//Overtaker: 76561199248893934(1065, 碧华). Old owner: 76561199348492078(180, 不是管理的卢本伟). FlagId: 19575. Location: X=193987.500 Y=-812988.750 Z=6623.212
	reg := regexp.MustCompile(`Overtaker:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+Old owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\.\s+Location:\s+(.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[3]
		streamId = strslice[1]
		flagType = "Abandoned on overtake"
		flagId = strslice[7]
		location = strslice[8]
		ownerId = strslice[5]
		ownerName = strslice[6]
		ownerStreamId = strslice[4]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 占领成功Overtaken.
func (r *LandFlagRule) HandleOvertakenDot(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//New owner: 76561198437146387(971, Vchan). Old owner: 76561199389134759(982, 牛牛). FlagId: 37712. Location: X=-865746.812 Y=-861001.125 Z=1373.193
	reg := regexp.MustCompile(`New owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+Old owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\.\s+Location:\s+(.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[3]
		streamId = strslice[1]
		flagType = "Overtaken"
		flagId = strslice[7]
		ownerId = strslice[5]
		ownerName = strslice[6]
		ownerStreamId = strslice[4]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 开始占领Overtake started
func (r *LandFlagRule) HandleOvertakeStart(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//User: 陌上清彤 (1263, 76561199401657420) Location: X=-470856.188 Y=-101563.617 Z=30967.398. Owner: 76561199246376362(766, 温凉). FlagId: 8154. Location: X=-470716.844 Y=-101588.297 Z=30875.250
	reg := regexp.MustCompile(`User:\s+(.+?)\s+\((\d+),\s+(.+?)\)\s+Location:\s+(.*)\s+Owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\..*`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[1]
		streamId = strslice[3]
		flagType = "Overtake started"
		flagId = strslice[8]
		location = strslice[4]
		ownerId = strslice[6]
		ownerName = strslice[7]
		ownerStreamId = strslice[5]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 占领时取消
func (r *LandFlagRule) HandleOvertakeCancel(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//User: 小捏捏 (1158, 76561199535610307). Owner: 76561198134126064(7, 管理员小兔). FlagId: 35654. Location: X=-53454.414 Y=157014.578 Z=72488.742
	reg := regexp.MustCompile(`User:\s+(.+?)\s+\((\d+),\s+(.+?)\)\.\s+Owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\.\s+Location:\s+(.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[1]
		streamId = strslice[3]
		flagType = "Overtake canceled"
		flagId = strslice[7]
		location = strslice[8]
		ownerId = strslice[5]
		ownerName = strslice[6]
		ownerStreamId = strslice[4]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}

// 占领，占领，离开小队/晋升
func (r *LandFlagRule) HandleOvertakenLeft(str string) (string, string, string, string, string, string, string, string, string, string) {
	actionTime := ""
	userId := ""
	userName := ""
	streamId := ""
	flagType := ""
	flagId := ""
	location := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//New owner: 76561199365511875(1357, 腰缠万贯). Old owner: 76561199445661380(513, 鱼). FlagId: 48986. Location: X=-185750.297 Y=-563735.812 Z=1766.163
	reg := regexp.MustCompile(`New owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+Old owner:\s+(.+?)\((\d+),\s+(.+?)\)\.\s+FlagId:\s+(\d+)\.\s+Location:\s+(.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) > 0 {
		actionTime = r.GetActionTime(str)
		userId = strslice[2]
		userName = strslice[3]
		streamId = strslice[1]
		flagType = "Overtaken (Left squad/Promoted)"
		flagId = strslice[7]
		location = strslice[8]
		ownerId = strslice[5]
		ownerName = strslice[6]
		ownerStreamId = strslice[4]
	}
	return actionTime, userId, userName, streamId,
		flagType, flagId, location, ownerId, ownerName, ownerStreamId
}
